package com.example.skyline.controller;

import com.example.skyline.Utils.Page;
import com.example.skyline.common.Result;
import com.example.skyline.entity.Query;
import com.example.skyline.entity.Role;
import com.example.skyline.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/getRoleList")
    public Result getRoleList(Query query) {
        Page page = new Page(query.getPageNum(), query.getPageSize(),roleService.getRoleCount());
        page.setList(roleService.getRoleListByRole(query.getStart(),
                query.getPageSize()));
        Map<String, Object> map = new HashMap<>();
        map.put("list", page.getList());
        map.put("count", page.getTotalCount());//总记录数
        map.put("pageNum", query.getPageNum());//当前页码
//        map.put("pageSize", query.getPageSize());//每页记录数
        return Result.success(map);
    }

    @PostMapping("/addRole")
    public Result addRole(@RequestBody Role role) {
        roleService.save(role);
        Map<String, Object> result = new HashMap<>();
        if (role != null) {
            result.put("code", 200);
            result.put("msg", "添加角色成功");
        } else {
            result.put("code", 444);
            result.put("msg", "添加角色失败");
        }
        return Result.success(result);
    }

    @PutMapping("/updateRole")
    public Result updateRole(@RequestBody Role role) {
        roleService.updateById(role);
        Map<String, Object> result = new HashMap<>();
        if (role != null) {
            result.put("code", 200);
            result.put("msg", "更新角色成功");
        } else {
            result.put("code", 444);
            result.put("msg", "更新角色失败");
        }
        return Result.success(result);
    }
    @GetMapping("/getRoleById/{id}")
    public Result getRoleById(@PathVariable Integer id) {
        Role role = roleService.getById(id);
        Map<String, Object> result = new HashMap<>();
        if (role != null) {
            result.put("code", 200);
            result.put("data", role);
        }else {
            result.put("code", 444);
            result.put("msg", "角色不存在");
        }
        return Result.success(result);
        }


    @DeleteMapping("/deleteRole/{id}")
    public Result deleteRole(@PathVariable Integer id) {
        roleService.removeById(id);
        Map<String, Object> result = new HashMap<>();
        if (id > 0) {
            result.put("code", 200);
            result.put("msg", "删除角色成功");
        } else {
            result.put("code", 444);
            result.put("msg", " 删除角色失败");
        }
        return Result.success(result);
    }

    @GetMapping("/selectRoleList")
    public Result getRoleList() {
        roleService.selectRoleList();
        return Result.success();
    }


}
